﻿using Ado.Interface;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace Ado.MsSql
{
    public class SqlHelper : IDbHelper
    {
        #region 执行查询，利用SqlDataReader返回结果并使用其进行一些操作+void ExecuteReader(string ConnStr,string strSql, out SqlDataReader reader, Action act, params SqlParameter[] paras)
        /// <summary>
        /// 执行查询，利用SqlDataReader返回结果并使用其进行一些操作
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="reader">SqlDataReader</param>
        /// <param name="paras">参数列表</param>
        /// <param name="act">操作</param>
        public void ExecuteReader(string ConnStr, string strSql, out SqlDataReader reader, Action act, params SqlParameter[] paras)
        {
            using (SqlConnection myConn = new SqlConnection(ConnStr))
            {
                myConn.Open();
                using (SqlCommand cmd = new SqlCommand(strSql, myConn))
                {
                    cmd.Parameters.AddRange(paras);//参数化
                    using (reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                           act.Invoke(); 
                        }
                    }
                }

            }
        } 
        #endregion
    }
}